<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
// This tests shift + selecting two discontinuous elements with user-select:
// all. Blink should select the both elements instead of moving the selection.
// To manually test, click to select the first element and shift-click the
// second element. WebKit should select both elements.

// TODO(editing-dev): Once http://crbug.com/736253 fixed, we should use
// |chrome.pointerActionSequence()| instead of |eventSender|.

const kStyle = [
  '<style>',
  '.select-all {',
    'border: 1px solid black;',
    'height: 100px;',
    'width: 100px;',
    'user-select: all;',
  '}',
  '</style>',
].join('');

const kSample = [
    kStyle,
    '<div class="select-all" id="first">First element</div>',
    'Some other text.',
    '<div class="select-all" id="second">Second element</div>',
].join('');

function clickOnElement(selection, id, keys) {
  const element = selection.document.getElementById(id);
  eventSender.mouseMoveTo(selection.computeLeft(element) + 10,
                          selection.computeTop(element) + 10);
  eventSender.mouseDown(0, keys);
  eventSender.mouseUp(0, keys);
}

function resetMouseClick() {
  eventSender.leapForward(1000);
  eventSender.mouseMoveTo(0, 0);
  eventSender.mouseDown();
  eventSender.mouseUp();
  eventSender.leapForward(1000);
}

function doTest(behavior) {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');
  assert_own_property(window, 'internals', 'This test requires internals.');
  internals.settings.setEditingBehavior(behavior);

  assert_selection(
    kSample,
    selection => {
      resetMouseClick();
      clickOnElement(selection, 'first');
    },
    [
      kStyle,
      '<div class="select-all" id="first">^First element|</div>',
      'Some other text.',
      '<div class="select-all" id="second">Second element</div>',
    ].join(''),
    `${behavior}-1: Click "first"`);

  assert_selection(
    kSample,
    selection => {
      resetMouseClick();
      clickOnElement(selection, 'first');
      clickOnElement(selection, 'second', ['shiftKey']);
    },
    [
      kStyle,
      '<div class="select-all" id="first">^First element</div>',
      'Some other text.',
      '<div class="select-all" id="second">Second element|</div>',
    ].join(''),
    `${behavior}-2: Shift+Click "second"`);

  assert_selection(
    kSample,
    selection => clickOnElement(selection, 'second'),
    [
      kStyle,
      '<div class="select-all" id="first">First element</div>',
      'Some other text.',
      '<div class="select-all" id="second">^Second element|</div>',
    ].join(''),
    `${behavior}-3: Click "second"`);

  assert_selection(
    kSample,
    selection => {
      clickOnElement(selection, 'second'),
      clickOnElement(selection, 'first', ['shiftKey']);
    },
    [
      kStyle,
      '<div class="select-all" id="first">|First element</div>',
      'Some other text.',
      '<div class="select-all" id="second">Second element^</div>',
    ].join(''),
    `${behavior}-4: Shift-Click "first"`);
}

for (const behavior of ['mac', 'win', 'unix']) {
  test(() => doTest(behavior),
       `${behavior}: Shift+Select on user-select:all`);
}
</script>
